﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DocumentFormat.OpenXml.Lite.Core.Shared
{
    public class Base64Binary
    {
        private string value = string.Empty;

        public Base64Binary() { }

        public Base64Binary(string value)
        {
            if (value.Replace(" ", "").Length % 4 != 0)
                throw new ArgumentException("Invalid base64Binary format (not divisible with 4)!");

            string regex = "[a-zA-z0-9+/= ]";
            if (!Regex.Match(value, regex).Success)
                throw new ArgumentException("Invalid base64Binary format (unknown character(s))!");

            if (value.IndexOf("=") != -1 && value.IndexOf("=") < value.Length - 2)
                throw new ArgumentException("Invalid base64Binary format (wrong '=' position)!");

            if (value.IndexOf("=") != -1 && value.IndexOf("=") == value.Length - 2 && value[value.Length - 1] != '=')
                throw new ArgumentException("Invalid base64Binary format (wrong '=' position)!");

            char[] allowedCharacters1 = { 'A', 'Q', 'g', 'w' };
            char[] allowedCharacters2 = { 'A', 'E', 'I', 'M', 'Q', 'U', 'Y', 'c', 'g', 'k', 'o', 's', 'w', '0', '4', '8' };
            if (value.EndsWith("==") && !allowedCharacters1.Contains(value[value.Length - 3]))
            {
                throw new ArgumentException("Invalid base64Binary format ('" +
                    value[value.Length - 3] + "' is not allowed at the end)!");
            }
            else if (!value.EndsWith("==") && value.EndsWith("=") && !allowedCharacters2.Contains(value[value.Length - 2]))
            {
                throw new ArgumentException("Invalid base64Binary format ('" +
                    value[value.Length - 3] + "' is not allowed at the end)!");
            }

            this.value = value;
        }

        public override bool Equals(object obj)
        {
            return (obj as Base64Binary).value == this.value;
        }

        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override string ToString()
        {
            return value;
        }

        public static bool operator ==(Base64Binary b1, Base64Binary b2)
        {
            return b1.value == b2.value;
        }

        public static bool operator !=(Base64Binary b1, Base64Binary b2)
        {
            return b1.value != b2.value;
        }

        public static Base64Binary operator +(Base64Binary b1, Base64Binary b2)
        {
            return new Base64Binary(b1.value + b2.value);
        }

        public static implicit operator string(Base64Binary b)
        {
            return b.value;
        }

        public static implicit operator Base64Binary(string s)
        {
            return new Base64Binary(s);
        }
    }
}
